﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InsuranceCompany.Helpers;
using InsuranceType = InsuranceCompany.Core.Domain.InsuranceArea.InsuranceType;
using PolicyC = InsuranceCompany.Core.Domain.InsuranceObjects.Policy;
using ClientNS = InsuranceCompany.Core.Domain.Client;
using InsuranceEvent = InsuranceCompany.Core.Domain.InsuranceObjects.InsuranceEvent;

namespace InsuranceCompany.Client
{
    public partial class Policy : InsuranceForm
    {
        private ObjectsCollection ITypesAll;
        private ClientNS.Client Client;
        public Policy()
        {
            InitializeComponent();
        }

        public Policy(ref List<ObjectsCollection> objects, ClientNS.Client client)
        {
            InitializeComponent();
            this.Objects = objects;
            this.Client = client;
            this.ITypesAll = this.Objects.Find(x => x.GetName() == "PolicyTypes");
            this.InsuranceTypesComboBox.Items.Add("Не выбрано");
            foreach (InsuranceType itype in this.ITypesAll.Items)
                this.InsuranceTypesComboBox.Items.Add(itype.Name);

            this.InsuranceTypesComboBox.SelectedIndex = 0;
            
        }

        private void SavePolicyButtonClick(object sender, EventArgs e)
        {
            if (this.Client != null)
            {
                PolicyC policy = new PolicyC();
                string itypeName = this.InsuranceTypesComboBox.SelectedItem.ToString();
                foreach (InsuranceType itype in this.ITypesAll.Items)
                {
                    if (itype.Name == itypeName)
                    {
                        policy.Type = itype;
                        break;
                    }
                }
                if (this.InsuranceSumTextBox.Text == "")
                    this.InsuranceSumTextBox.Text = "0";
                policy.InsuranceSum = Decimal.Parse(this.InsuranceSumTextBox.Text);
                if (this.PolicyPeriodTextBox.Text == "")
                    this.PolicyPeriodTextBox.Text = "0";
                policy.PolicyPeriod = Int32.Parse(this.PolicyPeriodTextBox.Text);
                if (this.PriceTextBox.Text == "")
                    this.PriceTextBox.Text = "0";
                policy.Price = Decimal.Parse(this.PriceTextBox.Text);
                policy.CreateDate = this.dateTimePicker1.Value;

                if (policy.Type != null)// && policy.InsuranceSum > policy.Type.RiskMaxSum)
                {
                    this.Client.AddEvent(new InsuranceEvent(policy));
                    this.Close();
                }
            }
        }
    }
}
